﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Tarea4
{
    class StateBase : StateInterface
    {
        protected short[,] board;
        protected static short[,] goalBoard;
        protected short index;

        public StateBase() { }

        public StateBase(short[,] Board)
        {
            board = (short[,])Board.Clone();   
            this.index = 0;

            goalBoard = new short[board.GetLength(0), board.GetLength(0)];

            short _index = 1;
            for (int i = 0; i < board.GetLength(0); i++)
            {
                for (int j = 0; j < board.GetLength(0); j++)
                {
                    goalBoard[i, j] = _index++;
                }
            }

            goalBoard[board.GetLength(0) - 1, board.GetLength(0) - 1] = 0;
        }

        public StateBase(short[,] Board, short index)
        {
            this.board = (short[,])Board.Clone();
            this.index = index;
        }

        public StateBase GetNewState(int row, int column)
        {
            int  newRow = -1, newColumn = -1;

            if (!(row >= 0 && row < board.GetLength(0) && column >= 0 && column < board.GetLength(0)))
                return null;
            if (row + 1 < board.GetLength(0) && board[row + 1, column] == 0)
            {
                newRow = row + 1;
                newColumn = column;
            }
            else if (row - 1 >= 0 && board[row - 1, column] == 0)
            {
                newRow = row - 1;
                newColumn = column;
            }
            else if (column - 1 >= 0 && board[row, column - 1] == 0)
            {
                newRow = row;
                newColumn = column - 1;
            }
            else if (column + 1 < board.GetLength(0) && board[row, column + 1] == 0)
            {
                newRow = row;
                newColumn = column + 1;
            }

            if (newRow == -1 && newColumn == -1)
                return null;
            else
            {
                short[,] tmpBoard = (short [,])this.Board.Clone();
                tmpBoard[newRow, newColumn] = tmpBoard[row, column];
                tmpBoard[row, column] = 0;
                StateBase newstate = new StateBase(tmpBoard, (short)(this.Index + 1));
                return newstate;
            }
            return null;
        }

        public int Heuristic
        {
            get { return 0; }
        }

        public bool IsGoalState()
        {
            for (int i = 0; i < board.GetLength(0); i++)
                for (int j = 0; j < board.GetLength(1); j++)
                    if (board[i, j] != goalBoard[i, j]) return false;

            return true;
        }

        public short[,] Board
        {
            get { return board; } 
        }


        public short[,] GoalBoard
        {
            get { return goalBoard; }
        }

        public short Index
        {
            get { return index; } 
        }
    }
}
